package com.zwx.string.leetcode_2;

/**
 * 颠倒整数
 * 给定一个32位有符号整数，将整数中的数字进行反转
 *
 * @author 文希
 * @create 2019-06-30 10:16
 */
public class ReverseInteger {

    private static Integer reverse(int x) {
        if (x == -Math.pow(2, 31))
            return 0;
        int temp = Math.abs(x);
        String s = Integer.toString(temp);
        StringBuilder builder = new StringBuilder(s);
        String result = builder.reverse().toString();
        if (Long.parseLong(result) > Integer.MAX_VALUE) {
            result = "0";
        }
        return x > 0 ? Integer.parseInt(result) : -Integer.parseInt(result);
    }

    public static void main(String[] args) {
        int x = -120;
        Integer reverse = reverse(x);
        System.out.println("reverse = " + reverse);
    }
}
